using System;
using System.ComponentModel;
using System.Transactions;

using SIS.ENT;
using SIS.DAL;


namespace SIS.BLL {

/// <summary>
/// The DayCodeTableManager class is responsible for managing BO.DayCodeTable objects in the system.
/// </summary>
[DataObjectAttribute()]
public class DayCodeTableManager
  {

#region "Public Methods"

/// <summary>
/// Gets a list with all DayCodeTable objects in the database.
/// </summary>
/// <returns>A list with all DayCodeTable from the database when the database contains any, or null otherwise.</returns>
[DataObjectMethod(DataObjectMethodType.Select, true)]
public static DayCodeTableList GetList(){
return DayCodeTableDB.GetList();
}

/// <summary>
/// Gets a single DayCodeTable from the database without its data.
/// </summary>
/// <param name="dayCode">The DayCode of the DayCodeTable in the database.</param>
/// <returns>A DayCodeTable object when the DayCode exists in the database, or <see langword="null"/> otherwise.</returns>
[DataObjectMethod(DataObjectMethodType.Select, false)]
public static DayCodeTable GetItem(short dayCode){
return GetItem(dayCode, false);
}

/// <summary>
/// Gets a single DayCodeTable from the database.
/// </summary>
/// <param name="dayCode">The DayCode of the DayCodeTable in the database.</param>
/// <param name="getDayCodeTableRecords">Determines whether to load all associated DayCodeTable records as well.</param>
/// <returns>
/// A DayCodeTable object when the DayCode exists in the database, or <see langword="null"/> otherwise.
/// </returns>
[DataObjectMethod(DataObjectMethodType.Select, false)]
public static DayCodeTable GetItem(short dayCode, bool getDayCodeTableRecords){
DayCodeTable myDayCodeTable = DayCodeTableDB.GetItem(dayCode);
if (myDayCodeTable != null && getDayCodeTableRecords){
myDayCodeTable.timeScheduleTables = TimeScheduleTableDB.GetListByDayCode(dayCode);
}
return myDayCodeTable;
}

/// <summary>
/// Saves a DayCodeTable in the database.
/// </summary>
/// <param name="myDayCodeTable">The DayCodeTable instance to save.</param>
/// <returns>The new DayCode if the DayCodeTable is new in the database or the existing DayCode when an item was updated.</returns>
[DataObjectMethod(DataObjectMethodType.Update, true)]
public static short Save(DayCodeTable myDayCodeTable){
using (TransactionScope myTransactionScope = new TransactionScope()){
short dayCodeTableDayCode = DayCodeTableDB.Save(myDayCodeTable);
foreach (TimeScheduleTable myTimeScheduleTable in myDayCodeTable.timeScheduleTables){
myTimeScheduleTable.DayCode = dayCodeTableDayCode;
TimeScheduleTableDB.Save(myTimeScheduleTable);
}

//  Assign the DayCodeTable its new (or existing DayCode).
myDayCodeTable.DayCode = dayCodeTableDayCode;

myTransactionScope.Complete();

return dayCodeTableDayCode;
}
}

/// <summary>
/// Deletes a DayCodeTable from the database.
/// </summary>
/// <param name="myDayCodeTable">The DayCodeTable instance to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
[DataObjectMethod(DataObjectMethodType.Delete, true)]
public static bool Delete(DayCodeTable myDayCodeTable){
return DayCodeTableDB.Delete(myDayCodeTable.DayCode);
}

#endregion

}

}
